package com.buka.goods.controller;


import com.buka.entity.Result;
import com.buka.goods.service.ProductDetailsService;
import com.buka.server.pojo.ProductDetails;
import com.buka.utils.BaseExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *商品详情页
 */
@RestController
@RequestMapping("/productDetails")
public class ProductDetailsController {
    @Autowired
    private ProductDetailsService productDetailsService;

    /**
     * 新增
     * @param productDetails
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody ProductDetails productDetails){
        Integer add = productDetailsService.add(productDetails);
        if (add != 1) {
            return new BaseExceptionHandler<>().error("新增失败");
        }
        return new Result<>("新增成功");
    }

    /**
     * 修改
     * @param productDetails
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody ProductDetails productDetails){
        Integer update = productDetailsService.update(productDetails);
        if (update != 1) {
            return new BaseExceptionHandler<>().error("修改失败");
        }
        return new Result<>("修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestParam Long id){
        Integer delete = productDetailsService.delete(id);
        if (delete != 1) {
            return new BaseExceptionHandler<>().error("删除失败");
        }
        return new Result<>("删除成功");
    }
    @GetMapping("/reducedInventory")
    public Result reducedInventory(@RequestParam Long id,@RequestParam Integer num){
        try {
            productDetailsService.reducedInventory(id,num);
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("减库存失败");
        }
        return new Result(true,"减库存成功");
    }
}